package com.leantech.android.browser.views

import android.content.Context
import android.content.res.Resources
import android.util.AttributeSet
import androidx.collection.SimpleArrayMap
import com.leantech.android.browser.R
import com.leantech.android.browser.skin.SkinManager
import com.cscj.android.utils.LOG
import com.google.android.material.tabs.TabLayout
import com.qmuiteam.qmui.skin.IQMUISkinHandlerView
import com.qmuiteam.qmui.skin.QMUISkinManager
import com.qmuiteam.qmui.skin.defaultAttr.IQMUISkinDefaultAttrProvider
import com.qmuiteam.qmui.util.QMUIResHelper

class SkinTabLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : TabLayout(context, attrs, defStyleAttr), IQMUISkinDefaultAttrProvider, IQMUISkinHandlerView {

    companion object {
        private val DefaultAttrs = SimpleArrayMap<String, Int>(3)

        const val KeyTabBackground = "tabBackground"
        const val KeyTabSelectedTextColor = "tabSelectedTextColor"
        const val KeyTabTextColor = "tabTextColor"


        init {
            DefaultAttrs.put(KeyTabBackground, R.attr.app_skin_state_bg_short_cut_tab)
            DefaultAttrs.put(KeyTabSelectedTextColor, R.attr.app_skin_tab_selected_text_color)
            DefaultAttrs.put(KeyTabTextColor, R.attr.app_skin_tab_text_color)
        }
    }


    override fun handle(
        manager: QMUISkinManager,
        skinIndex: Int,
        theme: Resources.Theme,
        attrs: SimpleArrayMap<String, Int>?
    ) {
        if (attrs == null || attrs.isEmpty()) return
        var tabSelectedTextColorAttr = 0
        var tabTextColorAttr = 0
        for (i in 0 until attrs.size()) {
            val key = attrs.keyAt(i)
            val attr = attrs.valueAt(i)

            when (key) {
                KeyTabBackground -> {
                    updateTabBackground(attr)
                }

                KeyTabSelectedTextColor -> {
                    tabSelectedTextColorAttr = attr
                }

                KeyTabTextColor -> {
                    tabTextColorAttr = attr
                }

                else -> {
                    manager.defaultHandleSkinAttr(this, theme, key, attr)
                }
            }

            LOG.d("SkinTabLayout", "attrs key ${attrs.keyAt(i)}, value ${attrs.valueAt(i)}")
        }

        if (tabSelectedTextColorAttr != 0 && tabTextColorAttr != 0) {
            updateSelectedTextColor(tabTextColorAttr, tabSelectedTextColorAttr)
        }
    }

    private fun updateTabBackground(attr: Int) {
        for (i in 0 until tabCount) {
            getTabAt(i)?.view?.background =
                QMUIResHelper.getAttrDrawable(context, SkinManager.currentTheme(context), attr)
        }
    }

    private fun updateSelectedTextColor(normalColorAttr: Int, selectedColorAttr: Int) {
        val normalColor =
            QMUIResHelper.getAttrColor(SkinManager.currentTheme(context), normalColorAttr)
        val selectedColor =
            QMUIResHelper.getAttrColor(SkinManager.currentTheme(context), selectedColorAttr)
        setTabTextColors(normalColor, selectedColor)
    }

    override fun getDefaultSkinAttrs(): SimpleArrayMap<String, Int>? {
        return DefaultAttrs
    }
}